package com.xbjtech.controller;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageInfo;
import com.xbjtech.model.BusinessUser;
import com.xbjtech.model.Order;
import com.xbjtech.model.OrderExample;
import com.xbjtech.model.OrderExample.Criteria;
import com.xbjtech.model.User;
import com.xbjtech.service.BusinessUserService;
import com.xbjtech.service.OrderService;
import com.xbjtech.util.AppConfig;

@Controller
@RequestMapping("/admin/order")
public class OrderController {
	
	@Autowired
	OrderService orderService;
	
	@Autowired
	BusinessUserService businessUserService;
	
	@GetMapping({"/list",""})
	public String list(
			@RequestParam(required = true, defaultValue="1") Integer currentPage, Model model) {
		OrderExample orderExample = new OrderExample();
		orderExample.setOrderByClause("id desc");
		Criteria criteria = orderExample.createCriteria();
		criteria.andDeletedNotEqualTo(1);
		PageInfo<Order> page = orderService.selectOrderListPage(orderExample, currentPage);
		model.addAttribute("page", page);
		return "admin/order/list";
	}
	
	@GetMapping("/delete")
	public String deleteById(@RequestParam Long id, RedirectAttributes redirectAttributes) {
		try {
			orderService.deleteById(id);
			redirectAttributes.addFlashAttribute("successMessage", "删除成功！");
			// 删除成功
		} catch (SQLException e) {
			// 删除失败
			redirectAttributes.addFlashAttribute("errorMessage", "删除失败！");
		}
		return "redirect:/admin/order/list";
	}
	
	@GetMapping("/add")
	public String add(Model model) {
		List<BusinessUser> businessUsers = businessUserService.selectAll();
		model.addAttribute("businessUsers", businessUsers);
		model.addAttribute("order", new Order());
		return "/admin/order/input";
	}
	
	@PostMapping("/save")
	public String save(HttpSession session, Order order, RedirectAttributes redirectAttributes) {
		try {
			User user = (User) session.getAttribute(AppConfig.LOGIN_USER_SESSION);
			if (null != order) {
				if (null == order.getId()) {
					order.setCreateid(user.getId());
					order.setState(0L);
					order.setCreatetime(new Date());
					order.setDeleted(0);
				}
			}
			orderService.merge(order);
			redirectAttributes.addFlashAttribute("successMessage", "保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("errorMessage", "保存失败！");
		}
		return "redirect:/admin/order/list";
	}
	
	@GetMapping("/view/{id}")
	public String view(@PathVariable Long id, Model model) {
		Order order = orderService.selectById(id);
		model.addAttribute("order", order);
		return "/admin/order/view";
	}
	
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable Long id, Model model) {
		Order order = orderService.selectById(id);
		List<BusinessUser> businessUsers = businessUserService.selectAll();
		model.addAttribute("businessUsers", businessUsers);
		model.addAttribute("order", order);
		return "/admin/order/input";
	}
	
	@ModelAttribute
	public void getModel(@RequestParam(required = false) Long id, Model model,HttpServletRequest request) {
		if (null != id) {
			Order order = orderService.selectById(id);
			order.setUpdatetime(new Date());
			order.setCreateid(((User)request.getSession().getAttribute(AppConfig.LOGIN_USER_SESSION)).getId());
			model.addAttribute("order", order);
		}
	}
}
